package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.UmsMemberInviteHistoryMapper;
import com.fc.mall.mapper.UmsMemberLevelMapper;
import com.fc.mall.model.UmsMember;
import com.fc.mall.model.UmsMemberInviteHistoryExample;
import com.fc.mall.model.UmsMemberLevel;
import com.fc.mall.portal.dto.GradeConditon;
import com.fc.mall.portal.service.UmsMemberLevelService;
import com.fc.mall.portal.service.UmsMemberService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Sunny on 2019/6/28.
 */
@Service
public class UmsMemberLevelServiceImpl implements UmsMemberLevelService {

    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private UmsMemberLevelMapper umsMemberLevelMapper;

    @Autowired
    private UmsMemberInviteHistoryMapper memberInviteHistoryMapper;

    @Autowired
    MapperFacade mapperFacade;


    @Override
    public GradeConditon getCurrentMemberLevleConditon()
    {
        GradeConditon gradeConditon = new GradeConditon();
        UmsMember member = umsMemberService.getCurrentMember();

        UmsMemberInviteHistoryExample example = new UmsMemberInviteHistoryExample();
        example.createCriteria().andMemberIdEqualTo(member.getId());
        Integer count = memberInviteHistoryMapper.countByExample(example);
        UmsMemberLevel memberLevel = umsMemberLevelMapper.selectByPrimaryKey(member.getMemberLevelId());
        mapperFacade.map(memberLevel, gradeConditon);
        UmsMemberLevel nextLevel = umsMemberLevelMapper.selectByPrimaryKey(memberLevel.getNextLevelId());
        if(null != nextLevel)
        {
            gradeConditon.setNextLevelCount(nextLevel.getInviteNumber().toString());
            gradeConditon.setNextCommissionFirstRate(nextLevel.getCommissionFirstRate().toString());
        }
        gradeConditon.setCurrentCondtion(count.toString());
        return gradeConditon;
    }

}
